package com.feri.rbacsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.rbacsys.dao.ExportOrderDao;
import com.feri.rbacsys.dao.ImportOrderDao;
import com.feri.rbacsys.dto.TorderDto;
import com.feri.rbacsys.entity.*;
import com.feri.rbacsys.mapper.CheckMapper;
import com.feri.rbacsys.mapper.OrderMapper;
import com.feri.rbacsys.service.intf.TorderService;
import com.feri.rbacsys.mapper.TorderMapper;
import com.feri.rbacsys.util.AuthUtil;
import com.feri.rbacsys.vo.R;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 24122
* @description 针对表【t_torder】的数据库操作Service实现
* @createDate 2023-03-28 16:05:05
*/
@Service
public class TorderServiceImpl extends ServiceImpl<TorderMapper, Torder>
    implements TorderService{
@Resource
TorderMapper torderMapper;
    @Resource
    CheckMapper checkMapper;
    @Resource
    StringRedisTemplate template;
    @Resource
    OrderMapper orderMapper;
    @Resource
    ExportOrderDao exportOrderDao;
    @Resource
    ImportOrderDao importOrderDao;
    @Override
    public R getTorderList() {
        List<TorderDto> torderDtos = torderMapper.getTorderDtos();
        return R.ok(torderDtos);
    }

    @Override
    @Transactional
    public R refuseTorder(Integer tid) {
        UpdateWrapper<Torder> wrapper = new UpdateWrapper<>();
        wrapper.set("flag",1);
        wrapper.eq("id",tid);
        int update = torderMapper.update(null, wrapper);

        if(update>0){
            Check check = new Check();
            int uid = AuthUtil.getUid();
            check.setCid(uid);
            check.setType(1);
            check.setOid(tid);
            checkMapper.insert(check);
            return R.ok();
        }
        return R.fail("fail");
    }

    @Override
    @Transactional
    public R allowedTorder(Integer tid) {
        if(template.hasKey("statuse")){
            int statuse = Integer.valueOf(template.opsForValue().get("statuse"));
            Torder torder = torderMapper.selectById(tid);
            if(statuse==1){
                orderMapper.deleteById(torder.getOid());
                torderMapper.deleteById(tid);
                return R.ok();
            }if(statuse==2){
                UpdateWrapper<Torder> wrapper = new UpdateWrapper<>();
                wrapper.set("flag",1);
                wrapper.eq("id",tid);
                int update = torderMapper.update(null, wrapper);
                if(update>0){
                    Check check = new Check();
                    int uid = AuthUtil.getUid();
                    check.setCid(uid);
                    check.setType(1);
                    check.setOid(tid);
                    checkMapper.insert(check);
                    QueryWrapper<ExportOrder>wrapper1=new QueryWrapper<>();
                    Order order = orderMapper.selectById(torder.getOid());
                    wrapper1.eq("type",0);
                    wrapper1.eq("order_num",order.getOrderNum());
                    exportOrderDao.delete(wrapper1);
                    return R.ok();
                }
                return R.fail();
            }if(statuse==3){
                UpdateWrapper<Torder> wrapper = new UpdateWrapper<>();
                wrapper.set("flag",1);
                wrapper.eq("id",tid);
                int update = torderMapper.update(null, wrapper);
                if(update>0){
                    Check check = new Check();
                    int uid = AuthUtil.getUid();
                    check.setCid(uid);
                    check.setType(1);
                    check.setOid(tid);
                    checkMapper.insert(check);
                    Order order=orderMapper.selectById(torder.getOid());
                    ImportOrder importOrder = new ImportOrder();
                    importOrder.setType(1);
                    importOrder.setOrderNum(order.getOrderNum());
                    importOrder.setAmount(order.getCount());
                    importOrder.setCtime(new Date());
                    importOrder.setProductId(order.getPid());
                    importOrder.setPrice(order.getPrice());
                    importOrderDao.insert(importOrder);
                    return R.ok();
                }
                return R.fail();
            }
        }

        return R.fail("未审核，无法同意");
    }
}




